package com.mayo.feaf;

import java.util.TreeSet;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.mayo.feaf.comun.BaseMainActivity;
import com.mayo.feaf.comun.expandablelist.Group;

public class MainActivity extends BaseMainActivity {

	private SlidingMenu slidingMenu;
	SparseArray<Group> groups = new SparseArray<Group>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        setBehindContentView(R.layout.slidingmenu);
        getSlidingMenu().setBehindOffset(R.dimen.slidingmenu_offset);
        
        getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_overflow));
        
        slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//        slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
//        slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.slidingmenu);
        
        final ListView listview = (ListView) slidingMenu.findViewById(R.id.listview);
        
        String[] values = new String[] {
        		getString(R.string.menu_entrada_noticias),
        		getString(R.string.app_name),
        		getString(R.string.menu_entrada_agrupaciones), 
        		getString(R.string.menu_entrada_festivales),
        		getString(R.string.menu_entrada_expo) };
        String[] descriptions = new String[] {
        		"",
        		"",
        		getString(R.string.menu_subentrada_comunidad), 
        		getString(R.string.menu_subentrada_circuito),
        		getString(R.string.menu_subentrada_indumentarias) };
        int[] icons = { 
        		R.drawable.ic_action_copy,
        		0,
        		R.drawable.dance, 
        		R.drawable.guitar, 
        		R.drawable.ic_launcher_old};
        Class[] clases = { 
        		MainActivity.class,
        		null,
        		AgrupacionesActivity.class, 
        		FestivalesActivity.class, 
        		IndumentariasActivity.class};
        
        MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(getApplicationContext(), values, descriptions, icons, clases);
        listview.setAdapter(adapter);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
     // Cargo la lista de noticias
        listaNoticias = (ListView) findViewById(R.id.lista_noticias);
        new LoadAllNoticias().execute();
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.slidingMenu.toggle();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  	  private final Context context;
  	  private final String[] values;
  	  private final String[] descriptions;
  	  private final Class[] clases;
  	  private final int[] icons;
  	  private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

  	  public MySimpleArrayAdapter(Context context, String[] values, String[] descriptions, int[] icons, Class[] clases) {
  	    super(context, R.layout.slidingmenu_layout, values);
  	    this.context = context;
  	    this.values = values;
  	    this.descriptions = descriptions;
  	    this.icons = icons;
  	    this.clases = clases;
  	  }

  	  @Override
  	  public View getView(final int position, View convertView, ViewGroup parent) {
  	    LayoutInflater inflater = (LayoutInflater) context
  	        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  	    View rowView = null;
  	    if (clases[position] == null)
  	    {
  	    	rowView = inflater.inflate(R.layout.list_header, null);
              TextView separador = (TextView)rowView.findViewById(R.id.texto_separador);
              separador.setText(values[position]);
  	    } else {
	    	    rowView = inflater.inflate(R.layout.slidingmenu_layout, parent, false);
	    	    TextView label = (TextView) rowView.findViewById(R.id.label);
	    	    TextView desc = (TextView) rowView.findViewById(R.id.desc);
	    	    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
	    	    label.setText(values[position]);
	    	    desc.setText(descriptions[position]);
	   	    	imageView.setImageDrawable(getResources().getDrawable(icons[position]));
	
	   	    	rowView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(), clases[position]);
						startActivity(intent);
					}
				});
  	    }
  	    return rowView;
  	  }
  }
}
